<?php

Class Controller_Report Extends Controller_Base {
	function index() {
		echo "report";
	}
	
	function getForAuthor(){
		if(!User::checkAccess("user"))die('Ошибка доступа');
		$d 				= $_POST;
		$id 			= $_SESSION['user']['person']['id'];
		if($id){
			$reports 		= Report::get("","",-1,"`author`='".$id."'");
			$res["title"] 	= "Ваши доклады";
			$res["html"] 	= "".Report::getViewData("",$reports);
			if(isset($d['ajax'])){
				echo json_encode($res);
			}else{
				echo $res['html'];
			}
		}
	}
	
	function removedList(){
		if(!User::checkAccess("developer"))die('Ошибка доступа');
		$d 				= $_POST;
		$reports 		= Report::get("","",-1,"`state`=-1");
		$res["title"] 	= "Удалённые доклады";
		if(count($reports)!=0){
			$res["html"] 	= "".Report::getViewData("",$reports);
		}else{
			$res['html'] 	= "";
			$res['msg'] 	= "Удалённых докладов нет!";
		}
		if(isset($d['ajax'])){
			echo json_encode($res);
		}else{
			echo $res['html'];
		}
	}
	
	function newList(){
		if(!User::checkAccess("user"))die('Ошибка доступа');
		$d 				= $_POST;
		$reports 		= Report::get("","",-1,"`state`=0");
		$res["title"] 	= "Новые доклады";
		if(count($reports)!=0){
			$res["html"] 	= "".Report::getViewData("",$reports);
		}else{
			$res['html'] 	= "";
			$res['msg'] 	= "Новых докладов нет!";
		}
		if(isset($d['ajax'])){
			echo json_encode($res);
		}else{
			echo $res['html'];
		}
	}
	
	function edit(){
		$d 	= $_POST;
		$db = DB::$db;
		$id = isset($d['id'])?$d['id']:"";
		if($id==""||Conference::getState()<0)die('Error 100010223');
		$report = Report::get("","",$id,"");
		$report = $report[0];
		if(!(User::checkAccess("admin")||User::checkAccess("",$report['author'])&&$report['state']==0))die('Ошибка доступа!!!');
		if(isset($d['form'])&&$d['form']){
			$report['tags']  	= "";
			$tags 				= Report::getReportTags(DB::$db,"",$id);
			$report['tags'] 	= Tag::getViewData($tags,"report","edit");
			$report['sections'] = Section::getSectionSelect(DB::get(),$report['section']);
			$edit_form  		= Template::parse("report/edit",$report);
			echo json_encode(array("html"=>$edit_form,"title"=>"Редактирование доклада - ".$report['name']));
		}else{
			$v = Report::validate($db,true);
			$v = Tag::validateAll($db, $_POST['tags'],$v);
			if($v['state']) {
				$tags 				= Report::getReportTags(DB::$db,"",$id);
				$new_tags			= $d['tags'];
				$real_tags 			= array();
				$repit_tags 		= array();
				foreach($new_tags as $i=>$k){
					if(isset($repit_tags[$k])){
						unset($new_tags[$i]);
					}else{
						$repit_tags[$k]=1;
					}
				}
				foreach ($tags as $prev_tag){
					$tag_id		= $prev_tag['id'];
					$deleteTag 	= true;
					foreach ($new_tags as $ind=>$new_tag) {
						if($tag_id==$new_tag){
							$deleteTag = false;
							unset($new_tags[$ind]);
							break;
						}
					}
					if($deleteTag){
						$reps = explode(",", $prev_tag['reports']);
						foreach ($reps as $i=>$report_id) {
							if($report_id==$id){
								unset($reps[$i]);
								break;
							}
						}
						$str 		= implode(",", $reps);
						if($str=="")$str="";
						$tag_data 	= array("reports"=>$str);
						Tag::set("",$tag_data,"",(int)$tag_id);
					}else{
						$real_tags[] = $tag_id;
					}
				}
				foreach ($new_tags as $new_tag) {
					$real_tags[] 	= $new_tag;
					$reports 		= Tag::get("","",$new_tag);
					$reports 		= $reports[0]['reports'];
					Tag::set("",array("reports"=>($reports!=""?$reports.",".$id:$id)),"",(int)$new_tag);
				}
				
				$data['tags'] 		= implode(",", $real_tags);
				$data['name'] 		= $d['name'];
				$data['section'] 	= $d['section'];
				$data['text'] 		= $d['text'];
				Report::set($db, $data,"",$id);
			 	$res 	= array("state" => true, "msg" => "Изменения сохранены!");
				echo json_encode($res);
			} else {
				echo json_encode($v);
			}
		}
	}
	
	function delete(){
		$id = isset($_POST['id'])?$_POST['id']:"";
		if($id==""||Conference::getState()<0)die('Error 100010224');
		$report = Report::get("","",$id,"",'`author`,`state`');
		if(!(User::checkAccess("admin")||User::checkAccess("",$report[0]['author'])&&$report[0]['state']==0))die('Ошибка доступа!!!');
		Report::set(DB::$db, array("state"=>'-1',"delete_date"=>date('Y-m-d H:i:s')),"",$id);
		echo json_encode(array("state"=>"1"));
	}
	
	function delete_final(){
		$id = isset($_POST['id'])?$_POST['id']:"";
		if($id=="")die('Error 100010223');
		if(!(User::checkAccess("admin")))die('Ошибка доступа!!!');
		Report::delete($id);
		echo json_encode(array("state"=>"1"));
	}
	
	function publicate(){
		$id = isset($_POST['id'])?$_POST['id']:"";
		if($id==""||Conference::getState()<0)die('Error 100010223');
		$report = Report::get("","",$id,"",'`author`,`state`');
		if(!(User::checkAccess("admin")&&$report[0]['state']==0))die('Ошибка доступа!!!');
		Report::set(DB::$db, array("state"=>'1',"publication_date"=>date('Y-m-d H:i:s')),"",$id);
		$person  	= Person::get("",$report[0]['author']);
		$user 		= $person['users'][0];
		if($user['state']!=1){
			User::set(array("state"=>'1'),$user['id'],true);
		}
		echo json_encode(array("state"=>"1"));
	}
	
	function new_() {
		if(!User::checkAccess('user')||Conference::getState()<0)die('Ошибка доступа!!!');
		$db = DB::get();
		$d 	= $_POST;
		if(isset($d['form']) == true) {
			$data['tags']		= Tag::count_()!=0?Tag::getViewData("empty","report","edit"):"";
			$data['sections']	= Section::getSectionSelect($db);
			$res["title"] 		= "Создание нового доклада";
			$res["html"] 		= Template::parse_("report/create",$data);
			echo json_encode($res);
		} else {
			$v = Report::validate($db);
			$v = isset($_POST['tags'])&&count($_POST['tags'])!=0?Tag::validateAll($db, $_POST['tags'],$v):$v;
			if($v['state']) {
				Report::create($db);
			 	$res 	= array("state" => true, "msg" => "Создание доклада выполнено!");
				echo json_encode($res);
			} else {
				echo json_encode($v);
			}
		}
	}
	

	function getByTag(){
		$d 				= $_POST;
		$id 			= isset($d['id'])?$d['id']:-1;
		$db 			= DB::get();
		if($id){
			$reports 		= Report::get($db,"",-1,"`tags` like '%".$id."%'");
			$res["title"] 	= "Доклад".($id>=0?" ".$reports[0]['name']:"");
			$res["html"] 	= "".Report::getViewData($db,$reports);
			if(isset($d['ajax'])){
				echo json_encode($res);
			}else{
				echo $res['html'];
			}
		}
	}
	
	function list_(){	
		$d 			= $_POST;
		$id 		= isset($d['id'])?$d['id']:-1;
		$db 		= DB::get();
		$reports	= Report::get($db,"",$id);
		$res["title"] 	= "Доклад - ".($id>=0?" ".$reports[0]['name']:"");
		$res["html"] 	= "".Report::getViewData($db,$reports,$id);
		//if($id!=-1)$res['callback']= 'function getMsg(){$.post("engine/msg.php",{report:'.$id.'},function(res){if(res&&res.state){$(".comments").append(res.html);getMsg();}},"json");};getMsg();';
		if(isset($d['ajax'])){
			echo json_encode($res);
		}else{
			echo $res['html'];
		}
	}
}
?>